package cn.tedu.store.portal.webapi.controller;

import cn.tedu.store.commons.pojo.portal.entity.PortalCategoryEntity;
import cn.tedu.store.commons.pojo.portal.vo.HomeElementsVO;
import cn.tedu.store.commons.pojo.portal.vo.PortalCategoryTreeVO;
import cn.tedu.store.commons.pojo.portal.vo.ProductDetailVO;
import cn.tedu.store.commons.pojo.product.vo.*;
import cn.tedu.store.commons.web.JsonPage;
import cn.tedu.store.commons.web.JsonResult;
import cn.tedu.store.portal.service.IHomeElementService;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 首页展示内容控制器
 *
 * @author RelentlessFlow
 * @version 0.0.1
 */
@Slf4j
@Api(tags = "01.首页展示相关")
@RestController
@RequestMapping("portal/homes")
public class HomeElementController {

    @Autowired
    private IHomeElementService homeElementService;

    @ApiOperation("获取首页推荐内容")
    @ApiOperationSupport(order = 100)
    @GetMapping("/item")
    public JsonResult<HomeElementsVO> getItem(){
        log.debug("开始处理[获取首页推荐内容]请求");
        return JsonResult.ok(homeElementService.getItem());
    }

    @ApiOperation("获取导航栏分类列表")
    @ApiOperationSupport(order = 110)
    @GetMapping("/navbar")
    public JsonResult<List<CategoryListItemVO>> getNavbarCategory(){
        log.debug("开始处理[获取导航栏分类]请求");
        return JsonResult.ok(homeElementService.navbarCategory());
    }

    @ApiOperation("获取分类树")
    @ApiOperationSupport(order = 115)
    @GetMapping("/tree")
    public JsonResult<PortalCategoryTreeVO<PortalCategoryEntity>> getCategoryTree(){
        log.debug("开始处理[获取分类树]请求");
        return JsonResult.ok(homeElementService.categoryTree());
    }

    @ApiOperation("获取子分类列表")
    @ApiOperationSupport(order = 120)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageSize", value = "每页记录数", dataType = "int"),
            @ApiImplicitParam(name = "pageNum", value = "页码", dataType = "int"),
            @ApiImplicitParam(name = "parentId", value = "父类id", required = true, dataType = "long")
    })
    @GetMapping("/sub-category-list")
    public JsonResult<JsonPage<CategoryListItemVO>> getBrandList(
            @RequestParam(required = false, defaultValue = "1") Integer pageNum,
            @RequestParam(required = false, defaultValue = "10") Integer pageSize,
            @RequestParam(required = false) Long parentId
    ){
        log.debug("开始处理[获取子分类列表]请求");
        return JsonResult.ok(homeElementService.subCategory(parentId, pageNum, pageSize));
    }

    @ApiOperation("获取品牌列表")
    @ApiOperationSupport(order = 130)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageSize", value = "每页记录数", dataType = "int"),
            @ApiImplicitParam(name = "pageNum", value = "页码", dataType = "int")
    })
    @GetMapping("/brand-list")
    public JsonResult<JsonPage<BrandListItemVO>> getBrandList(
            @RequestParam(required = false, defaultValue = "1") Integer pageNum,
            @RequestParam(required = false, defaultValue = "10") Integer pageSize

    ){
        log.debug("开始处理[获取品牌列表]请求");
        return JsonResult.ok(homeElementService.brandList(pageNum,pageSize));
    }

    @ApiOperation("获取商品列表")
    @ApiOperationSupport(order = 140)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageSize", value = "每页记录数", dataType = "int"),
            @ApiImplicitParam(name = "pageNum", value = "页码", dataType = "int")
    })
    @GetMapping("/product-list")
    public JsonResult<JsonPage<SpuListItemVO>> getProductList(
            @RequestParam(required = false, defaultValue = "1") Integer pageNum,
            @RequestParam(required = false, defaultValue = "10") Integer pageSize
    ){
        log.debug("开始处理[获取商品列表]请求");
        return JsonResult.ok(homeElementService.productList(pageNum,pageSize));
    }

    @ApiOperation("获取指定品牌的商品列表")
    @ApiOperationSupport(order = 150)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageSize", value = "每页记录数", dataType = "int"),
            @ApiImplicitParam(name = "pageNum", value = "页码", dataType = "int"),
            @ApiImplicitParam(name = "brandId", value = "品牌id", required = true, dataType = "long")
    })
    @GetMapping("/product-list-brand")
    public JsonResult<JsonPage<SpuListItemVO>> getProductListByBrand(
            @RequestParam(required = false, defaultValue = "1") Integer pageNum,
            @RequestParam(required = false, defaultValue = "10") Integer pageSize,
            @RequestParam(required = false) Long brandId
    ){
        log.debug("开始处理[获取指定品牌的商品列表]请求");
        return JsonResult.ok(homeElementService.productListByBrand(brandId, pageNum, pageSize));
    }

    @ApiOperation("获取指定分类的商品列表")
    @ApiOperationSupport(order = 160)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageSize", value = "每页记录数", dataType = "int"),
            @ApiImplicitParam(name = "pageNum", value = "页码", dataType = "int"),
            @ApiImplicitParam(name = "categoryId", value = "分类id", required = true, dataType = "long")
    })
    @GetMapping("/product-list-category")
    public JsonResult<JsonPage<SpuListItemVO>> getProductListByCategory(
            @RequestParam(required = false, defaultValue = "1") Integer pageNum,
            @RequestParam(required = false, defaultValue = "10") Integer pageSize,
            @RequestParam(required = false) Long categoryId
    ){
        log.debug("开始处理[获取指定分类的商品列表]请求");
        return JsonResult.ok(homeElementService.productListByCategory(categoryId, pageNum, pageSize));
    }

    @ApiOperation("获取商品详情")
    @ApiOperationSupport(order = 170)
    @ApiImplicitParam(name = "id", value = "商品id", required = true, dataType = "long")
    @GetMapping("/{id:[0-9]+}/product-detail")
    public JsonResult<ProductDetailVO> getDetailById(@PathVariable Long id){
        log.debug("开始处理[获取商品详情]请求");
        return JsonResult.ok(homeElementService.getDetailById(id));
    }

    @ApiOperation("获取商品SKU信息")
    @ApiOperationSupport(order = 180)
    @ApiImplicitParam(name = "spuId", value = "商品id", required = true, dataType = "long")
    @GetMapping("/{spuId:[0-9]+}/sku")
    public JsonResult<List<SkuListItemVO>> getSkuBySpuId(@PathVariable Long spuId){
        log.debug("开始处理[获取商品的sku]请求");
        return JsonResult.ok(homeElementService.getSkuBySpuId(spuId));
    }

    @ApiOperation("获取商品属性信息")
    @ApiOperationSupport(order = 190)
    @ApiImplicitParam(name = "spuId", value = "商品id", required = true, dataType = "long")
    @GetMapping("/{spuId:[0-9]+}/attribute")
    public JsonResult<List<AttributeListItemVO>> getAttributeBySpuId(@PathVariable Long spuId){
        log.debug("开始处理[获取商品属性]请求");
        return JsonResult.ok(homeElementService.getAttributeBySpuId(spuId));
    }
}
